<?php
declare (strict_types = 1);

namespace app\controller\api\v1\expert\trtc;

use app\controller\api\ApiController;
use app\exception\LogicException;
use app\facade\TrtcFacade;
use app\model\communal\TrtcRoomsModel;
use app\service\logic\common\order\OrderConsultService;
use app\service\logic\common\trtc\TrtcRoomsService;
use app\validate\common\order\ConsultValidate;
use app\validate\common\trtc\TrtcValidate;
use Exception;
use think\response\Json;

class TrtcController extends ApiController
{
    /**
     * trtc专家签名
     *
     * @return Json
     */
    public function expert()
    {
        $user = $this->request->middleware('user');
        $userId = config('app.app_name') . '_expert_' . $user['id'];
        $userSig = TrtcFacade::userSig($userId, 3600); // 3600s有效期
        return amis([
            'user_id' => $userId,
            'user_sig' => $userSig
        ]);
    }

    /**
     * trtc用户签名
     *
     * @return Json
     */
    public function user()
    {
        $params = $this->request->param();
        $this->validate($params, TrtcValidate::class . '.user');

        $userId = config('app.app_name') . '_user_' . $params['user_id'];
        $userSig = TrtcFacade::userSig($userId, 3600); // 3600s有效期
        return amis([
            'user_id' => $userId,
            'user_sig' => $userSig
        ]);
    }

    /**
     * 量表解读|心理资讯订单对应房间号
     *
     * @return Json
     */
    public function consult()
    {
        $params = $this->request->param();
        $this->validate($params, ConsultValidate::class . '.read');

        $result = invoke(OrderConsultService::class)->generateRoomNumber($params);
        return amis($result);
    }

    /**
     * 移除用户
     *
     * @return Json
     * @throws LogicException
     */
    public function rur()
    {
        $params = $this->request->param();
        $this->validate($params, TrtcValidate::class . '.rur');

        $userIds = explode(',', $params['user_ids']);
        if (count($userIds) > 10) {
            throw new LogicException('用户列表不能超过10个');
        }
        $result = TrtcFacade::removeUserRequest($params['room_id'], $userIds);
        return amis($result);
    }

    /**
     * 移出用户（字符串房间号）
     *
     * @return Json
     * @throws LogicException
     */
    public function rubsrir()
    {
        $params = $this->request->param();
        $this->validate($params, TrtcValidate::class . '.rur');

        $userIds = explode(',', $params['user_ids']);
        if (count($userIds) > 10) {
            throw new LogicException('用户列表不能超过10个');
        }
        $result = TrtcFacade::removeUserByStrRoomIdRequest($params['room_id'], $userIds);
        return amis($result);
    }

    /**
     * 解散房间
     *
     * @return Json
     */
    public function drr()
    {
        $params = $this->request->param();
        $this->validate($params, TrtcValidate::class . '.drr');

        $result = TrtcFacade::dismissRoomRequest($params['room_id']);
        return amis($result);
    }

    /**
     * 解散房间(根据专家)
     *
     * @return Json
     */
    public function drbe()
    {
        $user = $this->request->middleware('user');
        $roomIds = invoke(TrtcRoomsService::class)->getRoomIdsByMorIdType($user['id'], 2);
        $results = [];
        if (!empty($roomIds)) {
            foreach ($roomIds as $roomId) {
                try {
                    $results[] = TrtcFacade::dismissRoomRequest($roomId);
                } catch (Exception $e) {
                    // 解散房间异常
                    TrtcRoomsModel::where('room_id', $roomId)->delete();
                }
            }
        }

        return amis($results);
    }

    /**
     * 解散房间（字符串房间号）
     *
     * @return Json
     */
    public function drbsrir()
    {
        $params = $this->request->param();
        $this->validate($params, TrtcValidate::class . '.drr');

        $result = TrtcFacade::dismissRoomByStrRoomIdRequest($params['room_id']);
        return amis($result);
    }
}
